Imports Cdm.DataCore.UserSecurity

Partial Class AppHeaderWithTables
    Inherits System.Web.UI.UserControl

    Private isPrintPageValue As Boolean

    Private Property SettingsList() As Hashtable
        Get
            If ViewState("settingslist") Is Nothing Then
                SetSettingsList()
            End If
            Return ViewState("settingslist")
        End Get
        Set(ByVal value As Hashtable)
            ViewState("settingslist") = value
        End Set
    End Property

    Public Function GetAppSetting(ByVal settingName As String) As String
        If Me.SettingsList(settingName) Is Nothing Then
            Return ""
        Else
            Return Me.SettingsList(settingName).ToString()
        End If

    End Function

    Protected Sub Page_Load(ByVal sender As Object, _
                            ByVal e As System.EventArgs) _
                            Handles Me.Load



        If Not Me.IsPostBack Then

            Me.StateLogo.ImageUrl = GetAppSetting("StateLogoURL")
            Me.AppNameLabel.Text = GetAppSetting("ApplicationName")
            Me.DeptNameLabel.Text = GetAppSetting("StateDepartmentName")

            If Me.IsParticipantUser Then
                Me.ChangePasswordLink.Visible = False
                Me.HelpImageLink.Visible = False
                Me.LogoutImageLink.Visible = False
                Me.CCLogoImage.Visible = True
                Me.CCLogoTableData.Style("width") = "250px"
                Me.BlankTableRow.Style("height") = "0px"
                Me.ChangePasswordTableData.Style("width") = "0px"
                Me.HelpTableData.Style("width") = "0px"
                Me.LogoutTableData.Style("width") = "0px"
            End If

        End If

    End Sub

    Private Function IsParticipantUser() As Boolean

        Select Case (UserAuthorization.CurrentUser.RoleId)

            Case CInt(Role.RoleType.Participant), CInt(Role.RoleType.Representative), CInt(Role.RoleType.LegalGuardian)
                Return True
            Case Else
                Return False

        End Select

    End Function

    Private Sub SetSettingsList()
        Dim pageSettingsList As New Hashtable
        pageSettingsList.Add("ApplicationName", Application("AppName"))
        pageSettingsList.Add("StateDepartmentName", Application("StateDeptName"))
        pageSettingsList.Add("StateLogoURL", Application("CurrentLogo"))

        If UseLargeToolbar() Then
            pageSettingsList.Add("AppNameTop", "16px")
            pageSettingsList.Add("StateDeptTop", "50px")
            pageSettingsList.Add("StateDeptSize", ".75em")
            pageSettingsList.Add("AppNameSize", "1.5em")
            pageSettingsList.Add("StateLogoHeight", "67px")
            pageSettingsList.Add("CCLogoTop", "8px")
        Else
            pageSettingsList.Add("AppNameTop", "10px")
            pageSettingsList.Add("StateDeptTop", "34px")
            pageSettingsList.Add("StateDeptSize", ".7em")
            pageSettingsList.Add("AppNameSize", "1.2em")
            pageSettingsList.Add("StateLogoHeight", "57px")
            pageSettingsList.Add("CCLogoTop", "6px")
        End If

        Me.SettingsList = pageSettingsList
    End Sub

    Private Function UseLargeToolbar() As Boolean

        Select Case UserAuthorization.CurrentUser.RoleId

            Case Role.RoleType.LegalGuardian, Role.RoleType.Representative, Role.RoleType.Participant
                Return True
            Case Else
                Return False

        End Select

    End Function

    Public Sub SetPagePrintable()
        ChangePasswordLink.Visible = False
        HelpImageLink.Visible = False
        LogoutSpan.Visible = False
        NavLogOut.Visible = False

        Dim controlList As System.Web.UI.ControlCollection = Me.Page.Controls
        SetControlsPrintable(controlList)

        
    End Sub

    Private Sub SetControlsPrintable(ByVal controls As System.Web.UI.ControlCollection)

        Dim i As Integer

        For i = 0 To controls.Count() - 1
            Dim isInputControl As Boolean = EntityController.IsInputControl(controls(i))
            Dim isMenuControl As Boolean = EntityController.IsMenuControl(controls(i))
            Dim control As System.Web.UI.Control = controls(i)

            
            If isInputControl Then
                Dim currInputControl As InputControls.IInputControl = control
                currInputControl.Editable = False

                If currInputControl.Value = "" Then
                    currInputControl.Value = "_______________________"
                Else
                    FormatInputControl(currInputControl)
                End If
            End If

            If isMenuControl Then control.Visible = False

            If TypeOf controls(i) Is Button Then
                Dim bt As Button = controls(i)
                If bt.Text = "Details" Then
                    bt.Visible = True
                Else
                    bt.Visible = False
                End If
            End If

            If TypeOf (control) Is LinkButton Then
                Dim lnk As LinkButton = control
                If lnk.ID <> "PageLink" Then
                    lnk.Enabled = False
                End If
            End If

            If TypeOf (control) Is HyperLink Then
                Dim hyp As HyperLink = control
                hyp.Enabled = False
            End If

            If TypeOf controls(i) Is RadioButtonList Then
                Dim radioList As New RadioButtonList
                radioList = controls(i)
                radioList.Enabled = False
            End If

            If TypeOf controls(i) Is TextBox Then
                Dim textbox As New TextBox
                textbox = controls(i)
                textbox.BorderWidth = 0
            End If

            If TypeOf controls(i) Is DropDownList Then
                Dim dropdownList As New DropDownList
                dropdownList = controls(i)
                'dropdownList.Enabled = False
                Dim selectedItem As ListItem = dropdownList.SelectedItem
                dropdownList.Items.Clear()
                If Not selectedItem Is Nothing Then
                    dropdownList.Items.Add(selectedItem)
                End If
            End If

            If controls(i).HasControls() And Not isInputControl Then
                SetControlsPrintable(controls(i).Controls)
            End If
        Next

    End Sub

    Private Sub FormatInputControl(ByVal ctrl As InputControls.IInputControl)
        Select Case ctrl.ControlType.ToUpper()
            Case "SSN"
                ctrl.Value = String.Concat(ctrl.Value.Substring(0, 3), "-", ctrl.Value.Substring(3, 2), "-", ctrl.Value.Substring(5))
            Case "PHONE", "PHONEINPUT"
                ctrl.Value = String.Concat("(", ctrl.Value.Substring(0, 3), ") ", ctrl.Value.Substring(3, 3), "-", ctrl.Value.Substring(6))
        End Select
    End Sub

End Class
